package de.shortys.myhome.api.model;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class DataHolder implements IDataHolder {

	private DataHolderDescription description;

	private Map<String, Object> attributes;

	{
		attributes = new TreeMap<String, Object>();
	}

	public DataHolder(String descriptionPath) {
		// FIXME Exception?
		try {
			description = new DataHolderDescription(descriptionPath);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public DataHolderDescription getDescription() {
		return description;
	}

	public Object getValue(String key) {
		return attributes.get(key);
	}

	public void setValue(String key, Object value) {
		if (value != null || isNullable(key)) {
			attributes.put(key, value);
		} else {
			throw new IllegalArgumentException("attribute '" + key + "' may not be null");
		}
	}

	public Boolean isNullable(String key) {
		return description.isNullable(key);
	}

}
